using System;
using System.Collections.Generic;
using System.Text;
using Evolution.Engine.Interfaces;
using Microsoft.Xna.Framework;

namespace Evolution.Engine.Objects
{
    public class Fps : Evolution.Engine.Interfaces.IUpdateable
    {        
        private float _FPS = 0.0f;
        private float _TimeSinceLastUpdate = 0.0f;
        private float _Framecount = 0;
        private float _UpdateInterval = 1f;
        
        public float FramesPerSecond { get { return _FPS; } }

        public Fps()
        {
        }
   
        public override string ToString() 
        { 
            return string.Format("FPS: {0}", Math.Round(_FPS, 1)); 
        }

        public void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            _Framecount++;
            _TimeSinceLastUpdate += elapsed;
            if (_TimeSinceLastUpdate > _UpdateInterval)
            {
                //fps = 1 / elapsed;
                _FPS = _Framecount / _TimeSinceLastUpdate; //mean fps over updateIntrval
                _Framecount = 0;
                _TimeSinceLastUpdate -= _UpdateInterval;
            }
        }
    }
}
